package com.wgchao.identityrightsmanagement.query;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysBaseAccount;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysBaseAccountRepository;
import com.wgchao.identityrightsmanagement.infrastructure.utils.PageData;
import com.wgchao.identityrightsmanagement.query.pageparam.AccountPageParam;
import com.wgchao.identityrightsmanagement.query.vo.AccountVo;
import com.wgchao.identityrightsmanagement.query.vo.AccountVoConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: AccountQuery
 * @author: wgchao
 * @createTime: 2020/6/16 9:25 AM
 * @Description: TODO
 */
@Service
public class AccountQuery {
    @Autowired
    private SysBaseAccountRepository sysBaseAccountRepository;
    @Autowired
    private AccountVoConvert accountVoConvert;

    /**
     * 查询账户信息
     * @param userName
     * @return
     */
    public AccountVo findByUserName(String userName) {
        SysBaseAccount sysBaseAccount = sysBaseAccountRepository.findFirstByUsernameAndDelFalse(userName);
        if(sysBaseAccount == null){
            return null;
        }
        AccountVo accountVo = accountVoConvert.convert(sysBaseAccount);

        return accountVo;
    }

    /**
     * 条件分页查询
     * @param accountPageParam
     * @return
     */
    public PageData<AccountVo> findByConditionsOfPage(AccountPageParam accountPageParam){
        Specification<SysBaseAccount> specification = new Specification<SysBaseAccount>() {
            @Override
            public Predicate toPredicate(Root<SysBaseAccount> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if(!StringUtils.isEmpty(accountPageParam.getAgencyCode())){
                    Predicate predicate = criteriaBuilder.equal(root.get("agencyCode"), accountPageParam.getAgencyCode());
                    predicateList.add(predicate);
                }
                if(!StringUtils.isEmpty(accountPageParam.getDepartmentCode())){
                    Predicate predicate = criteriaBuilder.equal(root.get("departmentCode"), accountPageParam.getDepartmentCode());
                    predicateList.add(predicate);
                }
                if(!StringUtils.isEmpty(accountPageParam.getUsername())){
                    Predicate predicate = criteriaBuilder.equal(root.get("username"), accountPageParam.getUsername());
                    predicateList.add(predicate);
                }
                if(!StringUtils.isEmpty(accountPageParam.getName())){
                    Predicate predicate = criteriaBuilder.like(root.get("name"), "%"+accountPageParam.getName()+"%");
                    predicateList.add(predicate);
                }
                if(accountPageParam.getDisabled() != null){
                    if(accountPageParam.getDisabled()){
                        Predicate predicate = criteriaBuilder.isTrue(root.get("disabled"));
                        predicateList.add(predicate);
                    }else{
                        Predicate predicate = criteriaBuilder.isFalse(root.get("disabled"));
                        predicateList.add(predicate);
                    }
                }
                Predicate predicate = criteriaBuilder.isFalse(root.get("del"));
                predicateList.add(predicate);

                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        };
        Pageable pageable = PageRequest.of(accountPageParam.getCurr() - 1, accountPageParam.getLimit(), Sort.by(Sort.Order.desc("createTime")));

        Page<SysBaseAccount> accountPage = sysBaseAccountRepository.findAll(specification, pageable);

        if(accountPage.isEmpty()) {
            return new PageData<>(0L, accountPageParam.getCurr(), null);
        }
        List<AccountVo> accountVoList = accountPage.get().map(sysBaseAccount -> {
            return accountVoConvert.convert(sysBaseAccount);
        }).collect(Collectors.toList());

        return new PageData<>(accountPage.getTotalElements(), accountPageParam.getCurr(), accountVoList);
    }
}
